package com.seago.iceberg.config;

import com.seago.iceberg.repository.IcebergRepository;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * Title:
 * <p>
 * description:
 *
 * @Company: 中国船舶集团海舟系统技术有限公司-武汉
 * @author: yangs
 * @since: 2023-05-09 09:34
 * @version: 1.0
 **/
public class IcebergRepositoryFactoryBean<T extends IcebergRepository<?, ?>, S, ID> implements FactoryBean<T> {

	private Class<? extends T> repositoryInterface;
	private Class<S> entityType;
	private Class<ID> idType;

	public IcebergRepositoryFactoryBean(Class<? extends T> repositoryInterface, Class<S> entityType, Class<ID> idType) {
		this.repositoryInterface = repositoryInterface;
		this.entityType = entityType;
		this.idType = idType;
	}

	@Override
	public T getObject() throws Exception {
		IcebergRepositoryInvocationHandler<S, ID> handler = new IcebergRepositoryInvocationHandler<>(entityType, idType);
		return (T) Proxy.newProxyInstance(repositoryInterface.getClassLoader(), new Class[]{repositoryInterface}, handler);
	}

	@Override
	public Class<?> getObjectType() {
		return repositoryInterface;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}
}
